/**
 * 
 */
package com.leadership.api.web.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.leadership.api.service.IdmService;
import com.leadership.api.service.SearchIdmService;
import com.leadership.entity.idm.IdmOrg;
import com.leadership.entity.idm.IdmUser;

/**
 * @author lzj
 *
 */

@Controller
@RequestMapping("/search")
public class QueryIdmController {

    private static final Logger logger = LoggerFactory.getLogger(QueryIdmController.class);

    @Autowired
    @Qualifier("searchIdmService")
    private SearchIdmService searchIdmService;

    @Autowired
    private IdmService idmService;

    /**
     * 
     * @param filter
     * @return
     */
    @RequestMapping(value = "/idmUser", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult SearchIdmUserUser(String filter) {

        HttpResult result = new HttpResult();

        List<IdmUser> idmUsers = new ArrayList<IdmUser>();

        try {

            idmUsers = searchIdmService.QueryUser(filter);
            result.success().data("idmUser", idmUsers);

        } catch (Exception e) {
            logger.error("SearchIdmUserUser ", e);
            result.fail("500").message("发生异常");
        }

        return result;

    }

    @RequestMapping(value = "/new-user-test", method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult updateNewUser(String filter) {

        HttpResult result = new HttpResult();

        // 4. get user by orgnofullpath
        try {
            List<IdmUser> idmUsers = searchIdmService.QueryUser(filter);
            logger.info("get all idmUsers [{}]", JsonUtils.toJson(idmUsers));
            idmService.updateNewUser(idmUsers);
            result.success();

        } catch (UnsupportedEncodingException e) {
            logger.error("SearchIdmUserUser ", e);
            result.fail("500").message("发生异常");
        }

        return result;

    }

    /**
     * 
     * @param filter
     * @return
     */
    @RequestMapping(value = "/idmOrg", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult SearchIdmOrg(String filter) {

        HttpResult result = new HttpResult();

        List<IdmOrg> idmOrgs = new ArrayList<IdmOrg>();

        try {
            String encode = URLEncoder.encode(filter, "UTF-8");

            idmOrgs = searchIdmService.QueryOrg(encode);

            result.success().data("idmOrg", idmOrgs);

        } catch (Exception e) {
            logger.error("SearchIdmOrg- ", e);
            result.fail("500").message("发生异常");

        }

        return result;
    }
}
